package read;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;

/**
 *  Jsoup快速入门
 *
 * @author Lelezhang
 * @create 2020-07-04 16:20
 */
public class JsoupDemo1 {
    public static void main(String[] args) throws IOException {
        // 1.导入pom依赖
        // 2.获取document对象
        //2.1获取student.xml的path
        String path = JsoupDemo1.class.getClassLoader().getResource("abc.xml").getPath();
        //2.2解析xml文档，加载文档进内存，获取dom树--->Document
        Document document = Jsoup.parse(new File(path), "utf-8");
        //3.获取元素对象 Element
        Elements elements = document.getElementsByTag("name");

        System.out.println(elements.size());
        //3.1获取第一个name的Element对象
        Element element = elements.get(0);
        //3.2获取数据
        String name = element.text();
        String id = element.attr("id");
        System.out.println(name);
        System.out.println(id);
    }
}
